package com.chesnutconsulting.reports;

import java.util.ArrayList;
import java.util.List;

public class ReportCategory {

	String categoryId;
	String categoryName;
	List<ReportCategory> reportCategories = new ArrayList<ReportCategory>();
	List<Report> reports = new ArrayList<Report>();;

	public ReportCategory(String name, String id) {
		this.categoryName = name;
		this.categoryId = id;
	}

	public void addCategory(ReportCategory category) {
		// System.out.println("CATEGORY [" + this.getCategoryName() +
		// "] is adding category [" + category.getCategoryName() + "]");
		this.getReportCategories().add(category);
	}

	public void addReport(Report report) {
		// System.out.println("CATEGORY [" + this.getCategoryName() +
		// "] is adding report [" + report.getReportName() + "]");
		this.getReports().add(report);
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public List<ReportCategory> getReportCategories() {
		return reportCategories;
	}

	public void printTree(String tabs) {
		for (ReportCategory reportCategory : this.reportCategories) {
			reportCategory.printTree(tabs + "\t");
		}
		for (Report report : this.reports) {
			System.out.println(tabs + "\t\t" + report.toString());
		}
		System.out.println(tabs + "\t" + categoryName);
	}

	public List<Report> getReports() {
		return reports;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public List<Report> getReportLeaves() {
		List<Report> flattenedReports = new ArrayList<Report>();
		categoryRecurse(this, flattenedReports);
		return flattenedReports;
	}

	private void categoryRecurse(ReportCategory category,
			List<Report> list) {

		list.addAll(category.getReports());

		for (ReportCategory cat : category.getReportCategories()) {
			categoryRecurse(cat, list);
		}
		return;
	}
}
